package com.jn.exam.oss.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CopyObjectResult;
import com.aliyun.oss.model.PutObjectRequest;
import com.baidu.aip.util.Base64Util;
import com.jn.exam.UUIDUtil;
import com.jn.exam.oss.config.OssConfig;
import com.jn.exam.oss.service.OssService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.text.StringEscapeUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;


/**
 * @author 江南大学1033190417
 * @date 2021/12/16 17:11
 */
@Service
@RefreshScope
@Slf4j
public class OssServiceImpl implements OssService {

    //读取配置文件中的内容
    @Value("${aliyun.oss.file.endpoint}")
    private String endpoint;

    @Value("${aliyun.oss.file.keyid}")
    private String accessKeyId;

    @Value("${aliyun.oss.file.keysecret}")
    private String accessKeySecret;

    @Value("${aliyun.oss.file.bucketname}")
    private String bucketName;

    @Autowired
    private OSS oss;

    @Override
    public String uploadFileAvatar(MultipartFile file) {
        //OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //将上传的文件进行日期分类
        String time = new DateTime().toString("yyyy/MM/dd");
        String name = file.getOriginalFilename();
        String type = name.substring(name.lastIndexOf("."));
        String filename = time + "/" + UUIDUtil.getUUID() + type;
        oss.putObject(bucketName, filename, inputStream);
        // 关闭OSSClient。
        //ossClient.shutdown();
        //需要手动把阿里云oss路径手动拼接出来
        //https://msjava.oss-cn-qingdao.aliyuncs.com/01.jpg
        String url = "https://" + bucketName + "." + endpoint + "/" + filename;
        return url;
    }

    //上传人脸检测图像
    @Override
    public String uploadFaceImg(String img) {
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        String time = new DateTime().toString("yyyy/MM/dd");
        String filename = time + "/" + UUIDUtil.getUUID() + ".jpg";
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, filename, new ByteArrayInputStream(img.getBytes()));
        ossClient.putObject(putObjectRequest);
        ossClient.shutdown();
        String url = "https://" + bucketName + "." + endpoint + "/" + filename;
        return url;
    }

    //上传人脸检测图像
    @Override
    public String uploadFaceImgBytes(String img) {
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        String time = new DateTime().toString("yyyy/MM/dd");
        String filename = time + "/" + UUIDUtil.getUUID() + ".jpg";
        byte[] bytes = Base64Util.decode(img);
        ossClient.putObject(bucketName, filename, new ByteArrayInputStream(bytes));
        ossClient.shutdown();
        String url = "https://" + bucketName + "." + endpoint + "/" + filename;
        return url;
    }

    @Override
    public String uploadFaceImgBytes(byte[] bytes) {
        //OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        String time = new DateTime().toString("yyyy/MM/dd");
        String filename = time + "/" + UUIDUtil.getUUID() + ".jpg";
        oss.putObject(bucketName, filename, new ByteArrayInputStream(bytes));
        //ossClient.shutdown();
        String url = "https://" + bucketName + "." + endpoint + "/" + filename;
        return url;
    }

    //根据url删除
    @Override
    public boolean deleteFile(String url) {
        /*
        https://msjava.oss-cn-qingdao.aliyuncs.com/2022/01/25/dc1a9eff0cde4bb29f066f296495fcf4.jpg
         */
        String[] split = url.split("/");
        String objectName = split[3] + "/" + split[4] + "/" + split[5] + "/" + split[6];
        log.warn("删除图片：{}",url);
        oss.deleteObject(bucketName, objectName);
        return true;
    }

    //根据url赋值文件
    @Override
    public String copyFileByUrl(String url) {
        String[] split = url.split("/");
        String objectName = split[3] + "/" + split[4] + "/" + split[5] + "/" + split[6];
        String time = new DateTime().toString("yyyy/MM/dd");
        String type=split[6].substring(split[6].lastIndexOf("."));
        String newFileName=time+"/"+UUIDUtil.getUUID()+type;
        oss.copyObject(bucketName, objectName, bucketName, newFileName);
        String newUrl = "https://" + bucketName + "." + endpoint + "/" + newFileName;
        return newUrl;
    }
}
